#include "PlayerCountManager.h"

#include <climits>
#include "../share/user_define.h"

PlayerCountManager::PlayerCountManager(void)
{
	_safe_guard_begin;

	Clear();

	_safe_guard_end;
}

PlayerCountManager::~PlayerCountManager(void)
{
}

void PlayerCountManager::Clear(void)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> locker(m_mutex);

	m_player_count.clear();

	_safe_guard_end;
}

uint32_t PlayerCountManager::GetMinPlayerCountSceneId(void)
{
	_safe_guard_begin;

	std::lock_guard<std::recursive_mutex> locker(m_mutex);
	int32_t _min = INT_MAX;
	uint32_t _scene = 0;

	for (auto _it = m_player_count.cbegin(); _it != m_player_count.cend(); ++_it)
	{
		if (_it->second < _min)
		{
			_min = _it->second;
			_scene = _it->first;
		}
	}

	return _scene;

	_safe_guard_end;

	return 0;
}

void PlayerCountManager::UpdatePlayerCount(uint32_t sceneId, int32_t count)
{
	_safe_guard_begin;

//	printf("PlayerCountManager::UpdatePlayerCount %d : %d\n", sceneId, count);

	std::lock_guard<std::recursive_mutex> locker(m_mutex);

	auto _it = m_player_count.find(sceneId);
	if (_it != m_player_count.end())
	{
		m_player_count[sceneId] = count;
	}
	else
	{
		m_player_count.insert(std::make_pair(sceneId, count));
	}

	_safe_guard_end;
}
